$(function(){
	var d = new Date();
	var _year 		= d.getFullYear();
	var _month 		= Array("Janvier","Fevrier","Mars","Avril","Mai","Juin","Juillet","Aout","Septembre","Octobre","Novembre","Decembre");
	var _days 		= Array("Dimanche","Lundi","Mardi","Mercredi","Jeudi","Vendredi","Samedi");
	var fecha 		= _days[d.getDay()] +' '+ d.getDate()+' '+ _month[d.getMonth()]+' '+ d.getFullYear(); 
	var mes			= _month[d.getMonth()]+' '+ d.getFullYear(); 
	
	var _form 		= null;
	var _iduser 	= 0;
	var _mode 		= 'insert';
	var _idusers 	= Array();
	var _idT 		= "";	
	
	editorTXT();
	
	function editorTXT(){
		$('#txtcontenido').wysiwyg({
			controls:{
			separator00:{ visible: true },
			justifyLeft:{ visible: true },
			justifyCenter:{ visible: false },
			justifyRight:{ visible: false },
			justifyFull:{ visible: true },
			underline:{ visible: true },
			insertImage:{ visible: false },
			subscript:{ visible: false },
            superscript:{ visible: false },
			separator06:{ separator: false },
			h1:{ visible: false },
			h2:{ visible: false },
			h3:{ visible: false },
			h1mozilla:{ visible: false },
			h2mozilla:{ visible: false },
			h3mozilla:{ visible: false },
			word:{ exec: function(){} }
			}
		});
	}

	if(_intUserT!=1) {
		 _idT = '_id='+_intUserI+'&';
	}
	
	disabledControls(true);
	validateform();
	
	$("#jqGrid").jqGrid({
		// <MODIFICAR>
		url: './Queries/query-actualites.php?'+_idT+'nd='+new Date().getTime(),
		colNames:['int_actualite', 'Actualit&eacute;', 'Publi&eacute;', 'Titre', 'Sections', 'Vote', 'Etat'],
		colModel:[
			{hidden:true, search:false, name:'int_actualite', index:'int_actualite', align:'left'},
			{name:'var_monthyear', index:'var_monthyear', align:'left'},
			{name:'dat_creation_act', index:'dat_creation_act', align:'left'},
			{name:'var_ac_title', index:'var_ac_title', align:'left'},
			{name:'var_desc_section', index:'var_desc_section', align:'left'},
			{name:'int_vote', index:'int_vote', align:'center'},
			{name:'var_htmldesc', index:'var_htmldesc', align:'left'}
		],
		sortname: 'int_actualite',
		sortorder: "desc",
		width: 938,
		height: 400,
		// </MODIFICAR>
		
		loadtext: 'Chargement...',
		datatype: "json",
		pager: $('#jqpager'),
		rowNum:20,
		rowList:[10,20,50],	
		viewrecords: true,
		multiselect: true
	});
		
	function validateform(){
		
		_form = $("#frmnuevousuario").validate({
			event: "blur",
			rules:{ 
				'txttitle': {required: true},
				'txtcontenido': "required",
				'cboseccion': "required",
				'cboestado': "required"	
			},
			messages: {
				'txttitle': "Entrez votre la Titre, Minimum de 6 caract&egrave;res.",
				'txtcontenido': "Entrez votre la actualit&eacute;",
				'cboseccion': "S&eacute;lectionnez la section.",
				'cboestado': " S&eacute;lectionnez l'Etat."
			}
		});	
		
		// SAVE FORM 
		$("#frmnuevousuario").submit(function(){
			if(_form.valid()){
				disabledControls(false);
				save_nuevo();	
				disabledControls(true);
			}
			
			return false;
		});
	};
	
	$('#jqGrid').navGrid('#jqpager', {search:false, add:false,del:false,edit:false});	
	
	$("#supdate").click(function(){
		
		var s = $("#jqGrid").getGridParam('selarrrow');
		switch(s.length)
		{
			case 0:
				messageBox('Avertissement','S&eacute;lectionnez une actualit&eacute; parmi la liste.',"alert");
			break;
			
			case 1:
				_mode 	= 'update';
				_iduser = s; 
				 disabledControls( false );
				 defaultControls();
				 getUser( s );				 
				 ShowForm();
			break;
			
			default:
				messageBox('Avertissement','Seulement un actualit&eacute; &agrave; la fois. ',"alert");
			break;
		}
	});
	
	$("#sdelete").click(function(){
			_mode = 'delete';
			var gr = $("#jqGrid").getGridParam('selrow');
	
			if( gr != null ) 
			{
				var s = $("#jqGrid").getGridParam('selarrrow');
				var f = s.toString();
				var j =  f.split(',');
				_idusers = j;
				messageBoxConfirm("Eliminer l'Utilisateur", 'Vous avez selection&eacute; '+j.length+" actualit&eacute;(s).<br/>Voulez vous vraiment l'effacer(s)?", "alert");
			}
			else{
				messageBox('Avertissement','Seulement un actualit&eacute; &agrave; la fois.',"alert");
			}
	});
	
	$("#sinsert").click(function(){
		_mode = 'insert';
		disabledControls(false);
		defaultControls();
		ShowForm();
		// $('#txtcontenido').html('sadfsdfsdfsdfsdfsdfsdfsdf');
	});
	
	function defaultControls(){
		$('#txttitle').val('');
		$("#txtcontenido").wysiwyg("setContent", "");
		selectCbo( '#cboseccion', "" );
		selectCbo( '#cboestado', '' );
	}
	
	$('#s-dialog-cancelar4').click(function(){
		deleteUsers();
	});
	
	function deleteUsers(){
		
		var _newdata = "";
		for(var i = 0; i<_idusers.length; i++){
			_newdata += _idusers[i]+'-';
		}
		
		$.post("./?ws=Actualites&act=DeleteActualite", { _ids: _newdata, nocache: Math.random()}, function(data){

			if(data!="" || data!=" "){
				$('#jqGrid').trigger('reloadGrid');
				alert( $.trim(data) );
				$.modal.close();
			}
			
			$("#login-msg").ajaxError(function(request, settings){ 
				alert(settings.url );
			});
		});
	}
	
	
	
	function ShowForm(){
		if(_mode=='insert') $('#frm-title').html( "Nouveau l'Actualit&eacute;" );
		else $('#frm-title').html( "Modifier l'Actualit&eacute;" );
						
		$("#msg-nuevo").css( {display:'inline'} );	
		$('#txttitle').select();
	}
	
	function messageBox(_title, _msg, _type){
		var _src = './Resouces/images/alert.png';
		switch(_type){
			case "error":
				_src = './Resouces/images/error.png';
			break;
			case "info":
				_src = './Resouces/images/info.png';
			break;
		}
		
		$('#s-dialog-cancelar4').css( {display:'none'} );
		$('#s-dialog-cancelar3').val( 'Accepter' );
		$('#msg-title').html( _title );
		
		$('#msg-image').attr( 'src', _src );
		$('#msg-text').html( _msg );
		
		$("#msg-alert").modal({
			position: [0,0],
			opacity: 45,
			autoResize: true,
			onOpen: function (dialog){
				dialog.overlay.fadeIn('fast', function (){
					dialog.container.fadeIn('fast', function (){
						dialog.data.show();
					});
				});
			}			
		});
	}

	function messageBoxConfirm(_title, _msg, _type){
		var _src = './Resouces/images/alert.png';
		switch(_type){
			case "error":
				_src = './Resouces/images/error.png';
			break;
			case "info":
				_src = './Resouces/images/info.png';
			break;
		}
		
		$('#msg-title').html( _title );
		$('#s-dialog-cancelar4').css( {display:'inline'} );
		$('#s-dialog-cancelar3').val( 'Annuler' );
		
		$('#msg-image').attr( 'src', _src );
		$('#msg-text').html( _msg );
		
		$("#msg-alert").modal({
			position: [0,0],
			opacity: 45,
			autoResize: true,
			onOpen: function (dialog){
				dialog.overlay.fadeIn('fast', function (){
					dialog.container.fadeIn('fast', function (){
						dialog.data.show();
					});
				});
			}			
		});
	}
	
	$("#s-dialog-cancelar2").click(function(){
		$("#msg-nuevo").css( {display:'none'} );	
	});
	
	$("#s-dialog-cancelar3").click(function(){
		$.modal.close();
	});
			
	function disabledControls(val){
		if(val){
			$('#txttitle').attr('disabled','-1');
			$('#txtcontenido').attr('disabled','-1');
			$('#cboseccion').attr('disabled','-1');			
			$('#cboestado').attr('disabled','-1');
			$('#btnguardar').attr('disabled','-1');
		}
		else{
			$('#txttitle').removeAttr('disabled');
			$('#txtcontenido').removeAttr('disabled');
			$('#cboseccion').removeAttr('disabled');
			$('#cboestado').removeAttr('disabled');
			$('#btnguardar').removeAttr('disabled');
		}
	}
			
	function save_nuevo(){
		var _funct = 'InsertActualite';
		
		if(_mode == 'update'){
			_funct = 'UpdateActualite';
		}
		
		$.post("./?ws=Actualites&act="+_funct, { _id: _iduser, txttitle: $('#txttitle').val(), txtcontenido: $("#txtcontenido").wysiwyg("getContent").val(), cboseccion: $('#cboseccion').val(), cboestado: $('#cboestado').val(), dat_fecha:fecha, dat_mes:mes, nocache: Math.random()}, function(data){
			
			if(data!="" || data!=" "){
				$('#jqGrid').trigger('reloadGrid');
				alert( $.trim(data) );
				$("#msg-nuevo").css( {display:'none'} );
			}
			
			$("#login-msg").ajaxError(function(request, settings){ 
				alert(settings.url );
			});
		});
	}
	
	function getUser(_id){

		$.post("./?ws=Actualites&act=getActualite", { _id:_id, nocache: Math.random()}, function(data){

			if(data!="" || data!=" "){
				var _data = Array();
				_data = data.split('_%_');
				$('#txttitle').val( $.trim( _data[0] ) ); 
				$("#txtcontenido").wysiwyg("setContent", $.trim( _data[1] ));
				selectCbo('#cboseccion', _data[2]);
				selectCbo('#cboestado', _data[3]);
				$('#txttitle').select();
			}
						
			$("#login-msg").ajaxError(function(request, settings){ 
				alert(settings.url );
			});
		});
	}
	
	function selectCbo(_idcbo, _intval){
		$(_idcbo+" option").each(function (){
			if($(this).val() == _intval) $(this).attr('selected','selected'); 
		});
	}
	
});
